var prefix = jsctx + "/tenant/org";

var org = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    function load() {
        $('#exampleTable')
            .bootstrapTreeTable(
                {
                    id: 'orgId',
                    code: 'orgId',
                    parentCode: 'parentId',
                    type: "GET",
                    url: prefix + '/list',
                    // ajaxParams: {sort: 'sort'},
                    expandColumn: '1',
                    striped: true,
                    cache: false,
                    bordered: true,
                    expandAll: false,
                    clickToSelect: true,
                    // toolbar : '#exampleToolbar',
                    queryParams: function (params) {
                        return {
                            orgName: $('#orgName').val()
                        };
                    },
                    columns: [
                        {field: 'orgId', title: '编号'},
                        {field: 'orgName', title: '组织名称'},
                        {field: 'remarks', title: '说明'},
                        {
                            title: '操作',
                            field: 'orgId',
                            width:'100',align: 'center',
                            formatter: function (item, index) {
                                var e = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="org.edit(\''
                                    + item.orgId
                                    + '\')"><i class="fa fa-edit">编辑</i></a> ';
                                var a = '<a class="btn btn-primary btn-sm ' + has_add + '" href="#" title="增加下級"  onclick="org.add(\''
                                    + item.orgId
                                    + '\')"><i class="fa fa-plus">增加下級</i></a> ';

                                var d = '<a class="btn btn-warning btn-sm ' + has_delete + '" href="#" title="删除" onclick="org.remove(\''
                                    + item.orgId + '\',' + '\'' + item.orgName
                                    + '\')"><i class="fa fa-remove">删除</i></a> ';

                                // var d = '<a class="btn btn-warning btn-sm ' + has_delete + '" href="#" title="删除"  onclick="org.remove(\''
                                //     + item.orgId
                                //     + '\')"><i class="fa fa-remove">删除</i></a> ';
                                return e + a + d;
                            }
                        }]
                });
    }

    function reLoad() {
        load();
        //bootstrapTreeTable不能使用这方法刷新
        // $('#exampleTable').bootstrapTable('refresh');
    }

    /**
     * 增加
     */
    function add(parentId) {
        location.href = prefix + "/add/" + parentId;
    }

    /**
     * 修改
     * @param id
     */
    function edit(id) {
        location.href = prefix + "/edit/" + id;
    }

    /**
     * 删除
     * @param id
     */
    function remove(id, name) {
        layer.confirm('确定要删除选中的记录？' + name, {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/remove",
                type: "post",
                data: {
                    'id': id
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    /**
     * 返回列表
     */
    function goBack() {
        location.href = prefix + "/index";
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        add: add,
        edit: edit,
        remove: remove
    };
})();

var addOrg = (function () {
    function init() {
        validateRule();
    };

    function save() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save",
            data: $('#frmOrg').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("保存成功");
                    org.goBack();
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmOrg").validate({
            rules: {
                orgName: {
                    required: true
                }
            },
            messages: {
                orgName: {
                    required: icon + "请输入名称"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();

var editOrg = (function () {
    function init() {
        validateRule();
    };

    function update() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/update",
            data: $('#frmOrg').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("保存成功");
                    org.goBack();
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmOrg").validate({
            rules: {
                orgName: {
                    required: true
                }
            },
            messages: {
                orgName: {
                    required: icon + "请输入名称"
                }
            },
            submitHandler: function (form) {
                update();
            }
        })
    }

    return {
        init: init
    };
})();